package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.EcologyHatchMethodCollectionConfDao;
import com.sc.nft.dao.EcologyHatchMethodConfDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.req.EditEcologyHatchMethodCollectionConf;
import com.sc.nft.entity.vo.ecology.HatchMethodCollectionVO;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.EcologyHatchMethodCollectionConfService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 生态-熔炼方式藏品关联表服务接口实现
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-07 09:04:10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchMethodCollectionConfServiceImpl implements EcologyHatchMethodCollectionConfService {

    private final EcologyHatchMethodCollectionConfDao ecologyHatchMethodCollectionConfDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final EcologyHatchConfDao ecologyHatchConfDao;
    private final EcologyHatchMethodConfDao ecologyHatchMethodConfDao;

    /**
     * 编辑熔炼方式藏品关联表
     *
     * @param ecologyHatchId                         熔炼配置id
     * @param methodId                               熔炼方式id
     * @param ecologyHatchMethodCollectionConfVOList 藏品配置列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchMethodCollectionConf(Long ecologyHatchId, Long methodId, List<EditEcologyHatchMethodCollectionConf> ecologyHatchMethodCollectionConfVOList,Long collectionId) {
        long count = ecologyHatchMethodCollectionConfVOList.stream().map(EditEcologyHatchMethodCollectionConf::getCollectionId).distinct().count();
        if (count < ecologyHatchMethodCollectionConfVOList.size()) {
            throw new GlobalRunTimeException("同一个熔炼方式中，相同的生态藏品不能重复选择");
        }
        List<Long> list = ecologyHatchMethodCollectionConfVOList.stream().map(EditEcologyHatchMethodCollectionConf::getCollectionId).collect(Collectors.toList());
        if (!list.contains(collectionId)){
            throw new GlobalRunTimeException("未配置主藏品");
        }
        // 熔炼方式藏品关联表列表
        List<EcologyHatchMethodCollectionConf> ecologyHatchMethodCollectionConfList = getList(methodId, ecologyHatchId);
        List<Long> idsInDb = ecologyHatchMethodCollectionConfList.stream().map(EcologyHatchMethodCollectionConf::getId).collect(Collectors.toList());

        for (EditEcologyHatchMethodCollectionConf editEcologyHatchMethodCollectionConf : ecologyHatchMethodCollectionConfVOList) {
            // 获取熔炼方式藏品关联表id
            Long id = editEcologyHatchMethodCollectionConf.getId();
            // 校验
            DigitalCollection collection = digitalCollectionDao.getById(editEcologyHatchMethodCollectionConf.getCollectionId());
            Assert.notNull(collection, () -> new GlobalRunTimeException("数字收藏品不存在"));
            if (editEcologyHatchMethodCollectionConf.getDestroyCount().intValue() > 0) {
                Assert.isTrue(editEcologyHatchMethodCollectionConf.getDestroyCount().intValue() <= editEcologyHatchMethodCollectionConf.getExpendCount().intValue(), () -> new GlobalRunTimeException("回收数量不能大于消耗数量"));
            }
            // 熔炼方式藏品关联表
            if (Objects.isNull(id)) {
                EcologyHatchMethodCollectionConf ecologyHatchMethodCollectionConf = new EcologyHatchMethodCollectionConf();
                BeanUtils.copyProperties(editEcologyHatchMethodCollectionConf, ecologyHatchMethodCollectionConf);
                ecologyHatchMethodCollectionConf.setMethodId(methodId);
                ecologyHatchMethodCollectionConf.setEcologyHatchId(ecologyHatchId);
                ecologyHatchMethodCollectionConf.insert();
            } else {
                idsInDb.remove(id);
                EcologyHatchMethodCollectionConf ecologyHatchMethodCollectionConfInDb = ecologyHatchMethodCollectionConfDao.getOne(id, ecologyHatchId, methodId);
                Assert.notNull(ecologyHatchMethodCollectionConfInDb, () -> new GlobalRunTimeException("生态熔炼藏品配置不存在"));
                // 修改
                Assert.isTrue(ecologyHatchMethodCollectionConfDao.updateById(id, editEcologyHatchMethodCollectionConf.getCollectionId(), editEcologyHatchMethodCollectionConf.getCollectionName(), editEcologyHatchMethodCollectionConf.getExpendCount(), editEcologyHatchMethodCollectionConf.getDestroyCount()), () -> new GlobalRunTimeException("生态熔炼藏品配置更新失败"));
            }
        }
        if (!CollectionUtils.isEmpty(idsInDb)){
            ecologyHatchMethodCollectionConfDao.removeByIds(idsInDb);
        }
    }

    @Override
    public List<EcologyHatchMethodCollectionConf> getList(Long methodId, Long ecologyHatchId) {
        return ecologyHatchMethodCollectionConfDao.getList(methodId, ecologyHatchId);
    }

    /**
     * 获取熔炼配置藏品关联表
     *
     * @param ecologyHatchId
     * @return
     */
    @Override
    public List<HatchMethodCollectionVO> getHatchMethodCollectionVO(Long userId, Long ecologyHatchId) {
        return ecologyHatchMethodCollectionConfDao.getHatchMethodCollectionVO(userId, ecologyHatchId);
    }


    /**
     * 修改熔炼方式藏品配置 主藏品脚本
     * 每个方式 都默认加一个熔炼藏品作为主藏品
     */
    @Override
    @Transactional
    public void updateHatchMethodCollectionConfIsMainByIdScript(){

        //List<EcologyHatchConf> hatchConfList = ecologyHatchConfDao.getAllList();
        //for (EcologyHatchConf ecologyHatchConf : hatchConfList) {
            // 熔炼方式
            List<EcologyHatchMethodConf> ecologyHatchMethodConf = ecologyHatchMethodConfDao.getAllList();
            for (EcologyHatchMethodConf hatchMethodConf : ecologyHatchMethodConf) {

                EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(hatchMethodConf.getEcologyHatchId());
                if (ObjectUtil.isNotNull(ecologyHatchConf)){
                    EcologyHatchMethodCollectionConf ecologyHatchMethodCollectionConf_ = ecologyHatchMethodCollectionConfDao.getOneByCollectionId(ecologyHatchConf.getCollectionId(), hatchMethodConf.getId());
                    if (ObjectUtil.isNotNull(ecologyHatchMethodCollectionConf_)){
                        Assert.isTrue(ecologyHatchMethodCollectionConfDao.updateIsMainById(ecologyHatchMethodCollectionConf_.getId(),Boolean.TRUE), () -> new GlobalRunTimeException("生态熔炼藏品配置主藏品更新失败"));
                    }else {
                        EcologyHatchMethodCollectionConf ecologyHatchMethodCollectionConf = new EcologyHatchMethodCollectionConf();
                        ecologyHatchMethodCollectionConf.setMethodId(hatchMethodConf.getId());
                        ecologyHatchMethodCollectionConf.setEcologyHatchId(ecologyHatchConf.getId());
                        ecologyHatchMethodCollectionConf.setCollectionId(ecologyHatchConf.getCollectionId());
                        ecologyHatchMethodCollectionConf.setCollectionName(ecologyHatchConf.getCollectionName());
                        ecologyHatchMethodCollectionConf.setExpendCount(0);
                        ecologyHatchMethodCollectionConf.setDestroyCount(0);
                        ecologyHatchMethodCollectionConf.setIsMain(Boolean.TRUE);
                        ecologyHatchMethodCollectionConf.insert();
                    }

                }
            }
        //}
    }

}